import { ruleType } from '@kedacom/react-next'
import { regularType } from '@kedacom/react-next/dist/types/Input/InnerInput'
import { IVirtualMeetingRoom } from './interface'
import Alert from '@/components/Alert'
import { t } from 'i18next'
import { regexEnum } from '@/utils'

type RuleTestKeys =
	| 'name'
	| 'livePassword'
	| 'encryptedKey'
	| 'rate'
	| 'cascadeReturnPara'
	| 'callInterval'
	| 'callTimes'

export const ruleTests: Record<
	RuleTestKeys,
	({ regex: RegExp; msg: string } | { required: true; msg: string } | { numberLimit: number[]; msg: string })[]
> = {
	name: [
		{
			regex: regexEnum.nameIllegal,
			msg: t('会议室名称存在非法字符，请重新输入'),
		},
		{
			regex: /^.{0,32}$/,
			msg: t('会议室名称不能超过32个字符'),
		},
	],
	livePassword: [
		{
			regex: /^[a-zA-Z0-9]*$/,
			msg: t('直播密码存在非法字符,请重新输入'),
		},
		{
			regex: /^([a-zA-Z0-9]{6})$/,
			msg: t('直播密码请输入6个字符，支持大小写字母和数字'),
		},
	],
	encryptedKey: [
		{
			regex: /^[A-Za-z0-9_.]*$/,
			msg: t('可输入16个字符，包括英文字母(大小写)、数字、"."、"_"'),
		},
	],
	rate: [
		{
			required: true,
			msg: t('自定义码率不能为空'),
		},
		{
			numberLimit: [64, 8192],
			msg: t('自定义码率范围为64K~8M'),
		},
	],
	cascadeReturnPara: [
		{
			required: true,
			msg: t('回传带宽不能为空'),
		},
		{
			numberLimit: [1, 1000],
			msg: t('回传带宽范围为：1M~1000M'),
		},
	],
	callInterval: [
		{
			required: true,
			msg: t('呼叫间隔不能为空'),
		},
		{
			numberLimit: [10, 300],
			msg: t('呼叫间隔为10~300秒'),
		},
	],
	callTimes: [
		{
			required: true,
			msg: t('呼叫次数不能为空'),
		},
		{
			numberLimit: [2, 99],
			msg: t('呼叫次数为2~99秒'),
		},
	],
}

export const getRule = (val: RuleTestKeys) => {
	let ruleTest = ruleTests[val]
	let obj: { required?: { msg: string }; regular?: regularType[] } = {}
	let regulars: regularType[] = []
	ruleTest.forEach((rule) => {
		if ('required' in rule) {
			obj.required = { msg: rule.msg }
		} else if ('numberLimit' in rule) {
			regulars.push({
				regex: (val: string) => {
					return Number(val) < rule.numberLimit[0] || Number(val) > rule.numberLimit[1] ? true : false
				},
				msg: rule.msg,
			})
		} else if ('regex' in rule) {
			regulars.push(rule)
		}
	})
	if (regulars.length !== 0) {
		obj.regular = regulars
	}
	return obj
}

export const checkRule = (data: IVirtualMeetingRoom) => {
	for (let item in ruleTests) {
		let rules = ruleTests[item as RuleTestKeys]
		let val = (data as any)[item]
		for (let rule of rules) {
			if (item === 'livePassword' && (data.livePassword.length === 0 || !data.enableLive)) {
				continue
			}
			if (item === 'cascadeReturnPara' && (!data.cascadeReturn || data.simpleCascade !== 1)) {
				continue
			}
			if (item === 'encryptedKey' && data.encryptionType !== 2) {
				continue
			}
			if (item === 'callTimes' && (data.callMode === 0 || data.callTimesUnlimited)) {
				continue
			}
			if (item === 'rate' && data._bitrateType !== 0) {
				continue
			}
			if (item === 'rate' && 'required' in rule && val === undefined) {
				//针对自定义码率判空的特别处理
				Alert(rule.msg)
				return false
			}
			if ('required' in rule && val === '') {
				Alert(rule.msg)
				return false
			} else if (
				'numberLimit' in rule &&
				(parseInt(val) < rule.numberLimit[0] || parseInt(val) > rule.numberLimit[1])
			) {
				Alert(rule.msg)
				return false
			} else if ('regex' in rule && !rule.regex.test(val)) {
				Alert(rule.msg)
				return false
			}
		}
	}
	return true
}
